package com.scan.plugin.plus.scanPluginPlus;

import android.content.Context;
import android.content.IntentFilter;
import android.util.Log;

import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;

import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;

/**
 * This class echoes a string called from JavaScript.
 */
public class ScanPluginPlus extends CordovaPlugin {

    private final String TAG = "ScanPluginPlus";
    private Barcode2D scanManager;
    private Context mContext;
    private CallbackContext scanCallbackContext;

    @Override
    public void initialize(org.apache.cordova.CordovaInterface cordova, org.apache.cordova.CordovaWebView webView) {
        super.initialize(cordova, webView);
        Log.d(TAG,"onScanResult initialize");
        mContext= cordova.getActivity().getApplicationContext();
        scanManager = new Barcode2D(mContext);
    }

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        Log.d(TAG,"execute action:"+action);
        if ("open".equals(action)) {
            openScan(callbackContext);
            return true;
        } else if ("close".equals(action)) {
            closeScan(callbackContext);
            return true;
        }else if ("startRead".equals(action)) {
            startRead(callbackContext);
            return true;
        }else if ("stopRead".equals(action)) {
            stopRead(callbackContext);
            return true;
        }else if ("startContinuousRead".equals(action)) {
            startContinuousRead(callbackContext);
            return true;
        }else if ("stopContinuousRead".equals(action)) {
            stopContinuousRead(callbackContext);
            return true;
        }else {
            return false;
        }
    }

    private void openScan(CallbackContext callbackContext) {
        if(scanManager!=null){
            scanCallbackContext = callbackContext;
            scanManager.open(mContext,this::onScanResult);
            callbackContext.sendPluginResult(createPluginResult("open scan"));
        }
    }
    private void closeScan(CallbackContext callbackContext) {
        if(scanManager!=null){
            scanManager.close(mContext);
            callbackContext.sendPluginResult(createPluginResult("close scan"));
        }
    }


    private PluginResult createPluginResult(String msg){
        PluginResult result = new PluginResult(PluginResult.Status.OK,msg);
        result.setKeepCallback(true);
        return result;
    }

    private void startRead(CallbackContext callbackContext) {
        if(scanManager!=null){
            scanManager.openScan();
            callbackContext.sendPluginResult(createPluginResult("read start"));
        }
    }
    private void startContinuousRead(CallbackContext callbackContext) {
        if(scanManager!=null){
            scanManager.startContinuousScan();
            callbackContext.sendPluginResult(createPluginResult("Continuous read start"));
        }
    }

    private void stopContinuousRead(CallbackContext callbackContext) {
        if(scanManager!=null){
            scanManager.stopContinuousScan();
            callbackContext.sendPluginResult(createPluginResult("Continuous read stop"));
        }
    }

    private void stopRead(CallbackContext callbackContext) {
        if (scanManager != null) {
            scanManager.closeScan();
            callbackContext.sendPluginResult(createPluginResult("read stop"));
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (scanManager != null) {
            scanManager.closeScan();
        }
    }

    public void onScanResult(String barcode) {
        Log.d(TAG,"onScanResult barcode--11--:"+barcode);
        if(scanCallbackContext!=null){
            Log.d(TAG,"onScanResult barcode--22--:"+barcode);
            scanCallbackContext.sendPluginResult(createPluginResult("ID:"+barcode));
        }
    }
}
